package logic

import (
	"VipSrv/global/model"
	"VipSrv/global/pkg"
	"context"
	"fmt"
	"time"

	"VipSrv/internal/svc"
	"VipSrv/vip"

	"github.com/zeromicro/go-zero/core/logx"
)

type VipExpireTipLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVipExpireTipLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VipExpireTipLogic {
	return &VipExpireTipLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VipExpireTipLogic) VipExpireTip(in *vip.VipExpireTipRequest) (*vip.VipExpireTipResponse, error) {
	// todo: add your logic here and delete this line
	var User model.User
	if err := model.DB.Where("id = ?", in.UserId).Find(&User).Error; err != nil {
		return nil, err
	}
	if User.VipExpire.Unix() > time.Now().Unix() && User.VipExpire.Unix() < time.Now().AddDate(0, 0, 3).Unix() {
		key := fmt.Sprintf("user:%d:tips", in.UserId)
		rabbitmq := pkg.NewRabbitMQSimple("" +
			key)
		rabbitmq.PublishSimple("您的会员即将到期")
		fmt.Println("发送成功！")
	}
	return &vip.VipExpireTipResponse{}, nil
}
